home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’93
/
Mystery Science Mac
/
INIT sources
/
hImain.c
next >
Wrap
C/C++ Source or Header
|
1993-09-01
|
4KB
|
206 lines
#include "MacHeaders7"
#include "Utils.h"
#include "MSM.h"
#include <Packages.h>
#undef SystemSevenOrLater
#define SystemSevenOrLater 1
#pragma parameter __D0 __NewGestalt(__D0,__A0)
pascal OSErr __NewGestalt(OSType selector,ProcPtr gestaltFunction) = 0xA3AD;
typedef struct sysz {
long space;
void *DRVR;
} sysz, **syszH;
/*** main function ***/
static void strip_pc(void) {
asm {
move.l (a7)+,d0
StripAddress
move.l d0,-(a7)
}
}
short gBootTime;
long Gest;
short *buffer, gBuf[120];
short SoundToPlay(void) {
if (buffer > gBuf) {
return *--buffer;
}
return 0;
}
void QueueSound(short snd) {
if (buffer != gBuf && gBuf[0] == snd) return;
if (buffer < &gBuf[100]) {
*buffer++ = snd;
}
}
static
pascal OSErr MSMGestalt(OSType selector, void **response) {
void *oldA4 = CSetUpA4();
if (selector == 'MSM3') {
*response = &MSMGestalt;
} else if (selector == 'back') {
*response = (void *) SoundToPlay();
} else {
SetA4(oldA4);
return gestaltUndefSelectorErr;
}
SetA4(oldA4);
return noErr;
}
static long myA5;
void main() {
register short resID;
register traps *Patcher;
uchar name[128];
strip_pc();
asm {
; lea main,a0 ; commented out; address of code segment is already in a0.
move.l a0,d0
StripAddress
move.l d0,a4
}
asm { /* fix up all the SetUpA4's */
RecoverHandle SYS
move.l a0,-(a7)
jsr FixA4
addq #4,a7
}
// don't load twice.
if (Gestalt('MSM3', &Gest) == noErr) return;
if (SysVersion < 0x0700) return; // system 7 at least, man!
if (keymap.shift) goto nogo; // shift key?
// OK, do some real loading. First, switch to the system heap.
SetZone(SystemZone());
// OK, detach the MSM region so we can punch it out of the desktop region.
DetachResource(gMysterious = (void *)GetResource('rgn!', -4048));
// set up a port that we can draw into.
asm {
move.l a5,-(a7)
move.l CurrentA5,-(a7)
lea myA5,a5
move.l a5,CurrentA5
pea thePort
InitGraf
move.l #sizeof(CGrafPort),d0
NewPtr SYS
move.l a0,MSMPort
move.l a0,-(a7)
cmpi.w #0x3FFF,ROM85
bhi.s @notII
OpenCPort
bra.s @mrg
notII:OpenPort
mrg:;
}
/* if (!MFExists) */ asm {
move.l (a7)+,CurrentA5
move.l (a7)+,a5
}
// Patch our traps. Set the addresses up, then loop betwee them all.
Patcher = Patches;
Patcher[myMenuSelect ].newAddress = (long) real_address(&pMenuSelect);
Patcher[myDraw1Control ].newAddress = (long) real_address(&pDraw1Control);
Patcher[myInitWindows ].newAddress = (long) real_address(&pInitWindows);
asm {
move.w (Patcher),d0
lp: cmpi.w #0xA800,d0
blt.s @os
tb: GetTrapAddress NEWTOOL
bra.s @ov
os: GetTrapAddress NEWOS
ov: move.w (Patcher)+,d0
move.l a0,(Patcher)+
move.l (Patcher),a0
tst.l (Patcher)+
beq.s @noptch
cmpi.w #0xA800,d0
blt.s @OS
TB: SetTrapAddress NEWTOOL
bra.s @noptch
OS: SetTrapAddress NEWOS
noptch: move.w (Patcher),d0
bne.s @lp
}
// prep our action-buffering routine.
buffer = gBuf;
// register our gestalt selector.
__NewGestalt('MSM3', (void *)&MSMGestalt); // ignore error code.
// set the zone back. (Actually, we should probably save it off and restore it
// to what it was, in case future OS versions don't have an application zone at
// INIT time.
SetZone(ApplicZone());
/* install the jGNE patch */
asm {
lea @ptch,a0
lea @pj+2,a1
move.l JGNEFilter,(a1)
move.l a0,JGNEFilter
}
if (0) { /* let's not execute the patch code... */
ptch: asm {
movem.l a0-a1/d0-d2,-(a7)
move.l a4,-(a7)
lea 'rga4',a4
move.l a1,-(a7)
move.w d0,-(a7)
jsr pJGNEfilter
addq #6,a7
move.l (a7)+,a4
movem.l (a7)+,a0-a1/d0-d2
pj: jmp 0x12345678
}
};
asm {
move.l a4,a0
RecoverHandle SYS
move.l a0,-(a7)
DetachResource
}
ShowINIT(-4064); // success!
goto unloadSI;
nogo:
ShowINIT(-4048);
goto unloadSI;
unloadSI:
UnloadA4Seg(&ShowINIT); // get rid of the ShowINIT code; we don't need it anymore.
out:;
SetZone(ApplicZone());
}